<?php

class Ecommerce_transactionController extends AdminController {

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function actionList() {
        $model = new ecom_transaction('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['ecom_transaction']))
            $model->attributes = $_GET['ecom_transaction'];

        $this->render('list', array(
            'model' => $model,
        ));
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id),
        ));
    }

    public function actionProcess() {
        $model = new ecom_transaction;

        if (isset($_POST['ecom_transaction'])) {

            $transaction_code = $_POST['ecom_transaction']['transaction_code'];

            $rs_update = Yii::app()->db->createCommand()->update('ecom_transaction', array(
                'transaction_is_approve' => '1',
                    ), "transaction_code=:code", array(':code' => $transaction_code)
            );

            if ($rs_update) {
                Yii::app()->user->setFlash('success', 'Transaction has been approved.');
                $this->redirect(array('list'));
            } else {
                Yii::app()->user->setFlash('error', 'Approval Transaction failed.');
                $this->redirect(array('process'));
            }
        }

        $this->render('process', array(
            'model' => $model,
        ));
    }

    public function actionPrint() {

        $transaction_code = function_lib::uri_segment(5);

        $html_content = '';
        if ($transaction_code != '') {
            $order = ecom_transaction::model()->find('transaction_code=:code', array(':code' => $transaction_code));

            if ($order != false) {
                $is_category = false;
                $order_detail = ecom_transaction_detail::model()->findAll('transaction_detail_transaction_id = ' . $order->transaction_id . '');
                if ($order_detail != false) {
                    $order_product_detail = ecom_transaction_detail::model()->find('transaction_detail_transaction_id=' . $order->transaction_id . '');
                    if ($order_product_detail != false) {
                        $order_product = ecom_products::model()->find('products_id=' . $order_product_detail->transaction_detail_products_id . '');
                        if ($order_product != false) {
                            $order_product_category = ecom_products_category::model()->find('products_category_id=' . $order_product->products_products_category_id . '');
                            if ($order_product_category != false) {
                                $is_category = true;
                            }
                        }
                    }
                }


                //die($order_product_detail->products_products_category_id);


                if ($is_category) {
                    $kategori = $order_product_category->products_category_type;
                    if ($kategori == 'REG') {
                        $kategori = 'Registrasi';
                    } else if ($kategori == 'UP') {
                        $kategori = 'Belanja Produk Upgrade';
                    } else if ($kategori == 'AUTO') {
                        $kategori = 'Belanja Produk Tutup Poin';
                    } else if ($kategori == 'VOUCHER') {
                        $kategori = 'Belanja Produk Voucher';
                    } else {
                        $kategori = 'Belanja Produk Cycle';
                    }
                }

                $pdf = Yii::createComponent('application.extensions.tcpdf.ETcPdf', 'P', 'cm', 'A4', true, 'UTF-8');
                $pdf->SetCreator(PDF_CREATOR);
                $pdf->SetAuthor("Esoftdream");
                $pdf->SetTitle("Delivery Order");
                $pdf->SetSubject("Delivery Order");
                $pdf->setPrintHeader(false);
                $pdf->setPrintFooter(false);
                $pdf->AliasNbPages();

                if(!$is_category){
                    $kategori = '-';
                }
                
                $content_header_company = '<div style="font-size:26pt; font-weight:bold;">GLOBAL INFINITE</div>';
                $content_header_address = '<div>Jl. Kasugengan Kidul No. 12 Jamblang, Cirebon 45155</div><hr />';
                $content_title = '<div align="center"><strong>DELIVERY ORDER</strong></div>';
                $content_note = '
                    <table border="0" cellspacing="0" cellpadding="0">
                        <tr>
                            <td width="50%" align="left">ID Member: ' . function_lib::get_network_mid($order->transaction_buyer_network_id) . '<br />
                            Nama Member: ' . function_lib::get_member_name($order->transaction_buyer_network_id) . '<br />
                            Jenis Transaksi:' . $kategori . '
                            </td>
                            <td width="50%" align="right">
                                Kode Transaksi: ' . $order->transaction_code . '<br />
                                Tanggal Transaksi: ' . function_lib::convert_date($order->transaction_datetime, 'text', '', 'ina') . '
                            </td>
                        </tr>
                    </table>';
                $content_note_left = '
                        Kode Member: ' . function_lib::get_network_mid($order->transaction_buyer_network_id) . '<br />
                        Nama Member: ' . function_lib::get_member_name($order->transaction_buyer_network_id) . '
                        ';
                $content_note_right = '
                        Kode Transaksi: ' . $order->transaction_code . '<br />
                        Tanggal Transaksi: ' . function_lib::convert_date($order->transaction_datetime, 'text', '', 'ina') . '
                        ';
                $content_table = '
                    <style>
                        .list table { width: 100%; border-collapse:collapse; }
                        .list table th { border:1px solid #111; font-weight:bold; }
                        .list table td { border:1px solid #111; }
                    </style>
                    <div class="list">
                        <table width="100%" cellpadding="3" cellspacing="0">
                            <tr>
                                <th align="center" width="5%">NO</th>
                                <th align="center" width="12%">KODE</th>
                                <th align="center" width="75%">PRODUK</th>
                                <th align="center" width="8%">QTY</th>
                            </tr>
                    ';
                $no = 1;
                $subtotal_price = $total_price = $total_quantity = 0;
                foreach ($order_detail as $row_order_detail) {
                    $subtotal_price = $row_order_detail->transaction_detail_price * $row_order_detail->transaction_detail_quantity;
                    $total_price += $subtotal_price;
                    $total_quantity += $row_order_detail->transaction_detail_quantity;
                    $content_table .= '
                            <tr>
                                <td align="right">' . $no . '.</td>
                                <td align="center">' . $row_order_detail->transaction_detail_products_code . '</td>
                                <td>' . $row_order_detail->transaction_detail_products_name . '</td>
                                <td align="right">' . $row_order_detail->transaction_detail_quantity . '</td>
                            </tr>
                        ';
                    $no++;
                }
                $content_table .= '
                            <tr>
                                <th colspan="3" align="right"><strong>TOTAL</strong></th>
                                <th align="right">' . number_format($total_quantity) . '</th>
                            </tr>
                        </table>
                    </div>
                    ';

                $content_text = '
                    <p><strong>Dengan tujuan pengiriman barang:</strong></p>
                    <p>Nama Penerima:<br />' . $order->transaction_recipient_name . '</p>
                    <p>Alamat pengiriman:<br />' . nl2br($order->transaction_recipient_address) . '<br /> Telp. ' . $order->transaction_recipient_phone . '</p>
                    ';

                $pdf->AddPage();

                // define barcode style
                $style = array(
                    'position' => 'R',
                    'align' => 'C',
                    'stretch' => false,
                    'fitwidth' => true,
                    'cellfitalign' => '',
                    'border' => false,
                    'hpadding' => 'auto',
                    'vpadding' => 'auto',
                    'fgcolor' => array(0, 0, 0),
                    'bgcolor' => false, //array(255,255,255),
                    'text' => true,
                    'font' => 'helvetica',
                    'fontsize' => 8,
                    'stretchtext' => 4
                );
                $pdf->SetFont("times", "", 10);
                // CODE 128 AUTO
                $pdf->write1DBarcode($order->transaction_code, 'C128', '', '', 5, 2, '', $style, 'T');

                $pdf->SetFont("times", "", 13);
                $pdf->writeHTML($content_header_company, true, false, false, false, '');
                $pdf->Ln(0.1);

                $pdf->SetFont("times", "", 9);
                $pdf->writeHTML($content_header_address, true, false, false, false, '');
                $pdf->Ln(0.2);

                $pdf->SetFont("times", "", 13);
                $pdf->writeHTML($content_title, true, false, false, false, '');
                $pdf->Ln(0.5);

                $pdf->SetFont("times", "", 11);
                $y = $pdf->getY();
                $pdf->writeHTML($content_note, true, false, false, false, '');
                $pdf->Ln(0);

                $pdf->SetFont("times", "", 11);
                $pdf->writeHTML($content_table, true, false, false, false, '');

                $pdf->SetFont("times", "", 11);
                $pdf->writeHTML($content_text, true, false, false, false, '');

                $pdf->lastPage();

                $pdf->Output("Transaksi_" . $transaction_code . ".pdf", "D");
            } else {
                Yii::app()->controller->redirect(array('/admin/system/ecommerce_transaction/list'));
            }
        } else {
            Yii::app()->controller->redirect(array('/admin/system/ecommerce_transaction/list'));
        }
    }

    public
            function loadModel($id) {
        $model = ecom_transaction::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

}
